package ute.mvc.dao;

import java.util.List;

import javax.persistence.FetchType;
import javax.persistence.criteria.JoinType;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ute.mvc.model.MyClass;
import ute.mvc.model.User;
@Repository("classDAO")
public class ClassDAOImpl implements ClassDAO{
	@Autowired
	SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public List<MyClass> getAllClass() {
		return sessionFactory.getCurrentSession()
				.createCriteria(MyClass.class)
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.list();
	}

	@SuppressWarnings("unchecked")
	public List<MyClass> findClassByName(String className) {
		return sessionFactory.getCurrentSession()
				.createCriteria(MyClass.class)
				.add(Restrictions.like("className", "%" + className + "%"))
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.list();
	}
	public void addClass(MyClass myClass) {
		sessionFactory.getCurrentSession().save(myClass);
	}

	public void updateClass(MyClass myClass) {
		sessionFactory.getCurrentSession().merge(myClass);
	}

	public void deleteClass(MyClass myClass) {
		sessionFactory.getCurrentSession().delete(myClass);
	}

	public MyClass getClassById(String classId) {
		return (MyClass) sessionFactory.getCurrentSession().get(MyClass.class, classId);
	}

	@SuppressWarnings("unchecked")
	public List<User> getListUserByClassId(String classId) {
		return sessionFactory.getCurrentSession()
				.createCriteria(User.class)
				.createCriteria("listClass")
				.add(Restrictions.eq("classId", classId))
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.list();
	}

	public MyClass getClassByIdWithListStudent(String classId) {
		return (MyClass) sessionFactory.getCurrentSession()
				.createCriteria(MyClass.class)
				.setFetchMode("listStudent", FetchMode.EAGER)
				.add(Restrictions.eq("classId", classId))
				.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<User> getAddUserById(String classId) {
		Session session = sessionFactory.getCurrentSession();
		System.out.println("1");
		List<String> userIds =  session.createCriteria(User.class)
				.setProjection(Projections.property("userId"))
				.createCriteria("listClass")
				.add(Restrictions.eq("classId", classId))
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.list();
		if(userIds.size() != 0) 
			return session.createCriteria(User.class)
					.add(Restrictions.eq("role", 3))
					.add(Restrictions.not(Restrictions.in("userId", userIds)))
					.list();
		else 
			return session.createCriteria(User.class)
					.add(Restrictions.eq("role", 3))
					.list();
	}
}
